<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>火星情报局 - 管理中心</title>
    <style>
        body {
            font-family: 'PingFang SC', 'Microsoft YaHei', sans-serif;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
            background: linear-gradient(135deg, #1a237e, #311b92);
            color: white;
            text-align: center;
        }
        .loading {
            display: flex;
            flex-direction: column;
            align-items: center;
        }
        .spinner {
            border: 4px solid rgba(255, 255, 255, 0.3);
            border-radius: 50%;
            border-top: 4px solid #fff;
            width: 40px;
            height: 40px;
            animation: spin 1s linear infinite;
            margin-bottom: 20px;
        }
        @keyframes spin {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }
        .manual-actions {
            margin-top: 30px;
            display: none;
        }
        .btn {
            background-color: white;
            color: #1a237e;
            border: none;
            padding: 10px 20px;
            border-radius: 4px;
            font-weight: bold;
            cursor: pointer;
            margin: 0 10px;
            transition: all 0.3s;
        }
        .btn:hover {
            background-color: #e0e0e0;
        }
        #errorMessage {
            color: #ff5252;
            margin-top: 20px;
            display: none;
        }
    </style>
</head>
<body>
    <div class="loading">
        <div class="spinner"></div>
        <h2>正在进入火星情报局管理中心</h2>
        <p id="statusMessage">检查登录状态...</p>
        
        <div id="errorMessage"></div>
        
        <div class="manual-actions" id="manualActions">
            <p>如果没有自动跳转，请选择：</p>
            <button class="btn" onclick="goToLogin()">登录页面</button>
            <button class="btn" onclick="goToDashboard()">管理页面</button>
        </div>
    </div>

    <script>
        // 检查存储系统可用性
        function checkStorageAvailability() {
            const status = {
                localStorage: false,
                sessionStorage: false,
                cookies: false
            };
            
            // 检查localStorage
            try {
                localStorage.setItem('test', 'test');
                localStorage.removeItem('test');
                status.localStorage = true;
            } catch (e) {
                console.error('localStorage不可用:', e);
            }
            
            // 检查sessionStorage
            try {
                sessionStorage.setItem('test', 'test');
                sessionStorage.removeItem('test');
                status.sessionStorage = true;
            } catch (e) {
                console.error('sessionStorage不可用:', e);
            }
            
            // 检查cookies
            try {
                document.cookie = "testcookie=1; path=/; max-age=60";
                status.cookies = document.cookie.indexOf("testcookie=") !== -1;
            } catch (e) {
                console.error('cookies不可用:', e);
            }
            
            return status;
        }
        
        // 获取token从所有存储位置
        function getToken() {
            let token = null;
            let source = '';
            
            // 按优先级检查各种存储
            if (localStorage && localStorage.getItem('adminToken')) {
                token = localStorage.getItem('adminToken');
                source = 'localStorage';
            } else if (sessionStorage && sessionStorage.getItem('adminToken')) {
                token = sessionStorage.getItem('adminToken');
                source = 'sessionStorage';
            } else {
                // 检查cookie
                const match = document.cookie.match(/adminToken=([^;]+)/);
                if (match) {
                    token = match[1];
                    source = 'cookies';
                }
            }
            
            if (token) {
                console.log(`从 ${source} 读取到token: ${token.substring(0, 15)}...`);
            } else {
                console.warn('未找到存储的token');
            }
            
            return token;
        }
        
        // 跳转到登录页
        function goToLogin() {
            try {
                window.location.href = '/admin/login.html';
            } catch (e) {
                showError('导航失败，请直接访问: /admin/login.html');
            }
        }
        
        // 跳转到管理页
        function goToDashboard() {
            try {
                window.location.href = '/admin/dashboard.html';
            } catch (e) {
                showError('导航失败，请直接访问: /admin/dashboard.html');
            }
        }
        
        // 显示错误信息
        function showError(message) {
            document.getElementById('errorMessage').style.display = 'block';
            document.getElementById('errorMessage').textContent = message;
            document.getElementById('manualActions').style.display = 'block';
        }
        
        // 执行路由检查
        function checkAndRoute() {
            document.getElementById('statusMessage').textContent = '检查登录状态...';
            
            // 检查存储可用性
            const storageStatus = checkStorageAvailability();
            console.log('存储系统状态:', storageStatus);
            
            if (!storageStatus.localStorage && !storageStatus.sessionStorage && !storageStatus.cookies) {
                document.getElementById('statusMessage').textContent = '您的浏览器不支持任何存储机制';
                showError('无法检查或保存登录状态，请尝试更换浏览器或退出隐私模式');
                return;
            }
            
            // 检查是否有token
            const token = getToken();
            
            setTimeout(function() {
                try {
                    if (token) {
                        // 已登录，跳转到管理页面
                        document.getElementById('statusMessage').textContent = '已检测到登录状态，正在跳转...';
                        window.location.href = '/admin/dashboard.html';
                    } else {
                        // 未登录，跳转到登录页
                        document.getElementById('statusMessage').textContent = '未检测到登录状态，请先登录';
                        window.location.href = '/admin/login.html';
                    }
                } catch (e) {
                    // 如果跳转出错
                    showError('导航失败: ' + e.message);
                }
            }, 1000);
            
            // 3秒后如果仍在此页面，显示手动选项
            setTimeout(function() {
                if (window.location.href.includes('/admin/index.html') || 
                    window.location.href.endsWith('/admin/')) {
                    document.getElementById('manualActions').style.display = 'block';
                    document.getElementById('statusMessage').textContent = '自动跳转失败，请手动选择';
                }
            }, 3000);
        }
        
        // 页面加载后执行
        document.addEventListener('DOMContentLoaded', checkAndRoute);
    </script>
</body>
</html> 